<template>
  <div class="addRole">
    <el-dialog
      title="添加角色"
      :visible.sync="addDialogVisible"
      width="40%"
      :center="true"
      :before-close="addRoleCancel"
    >
      <el-form
        label-width="140px"
        :model="roleList"
        :rules="rules"
        ref="roleList"
      >
        <el-form-item label="角色名称(必填)" prop="rname">
          <el-input placeholder="请输入" v-model="roleList.rname"></el-input>
        </el-form-item>
        <el-form-item label="角色标识(必填)" prop="code">
          <el-input placeholder="请输入" v-model="roleList.code"></el-input>
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input
            type="textarea"
            :rows="6"
            v-model="roleList.describe"
            placeholder="请输入内容"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="角色状态(必填)" prop="state">
          <el-select v-model="roleList.state">
            <el-option
              v-for="item in stateLsit"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRoleCancel">取 消</el-button>
        <el-button type="primary" @click="addRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "AddRole",
  props: ["addDialogVisible"],
  data() {
    return {
      //             角色名称         角色状态          角色标识       角色描述
      roleList: { rname: "", state: 0, code: "", describe: "" },
      rules: {
        rname: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
        state: [{ required: true, message: "请选择角色状态", trigger: "blur" }],
        code: [{ required: true, message: "请输入角色标识", trigger: "blur" }],
      },
      stateLsit: [
        { name: "启用", id: 0 },
        { name: "禁用", id: 1 },
      ],
    };
  },
  methods: {
    // 取消新增按钮
    addRoleCancel() {
      this.$emit("addRoleCancel", false);
      this.roleList = { rname: "", state: 0, code: "", describe: "" };
      this.$message("已取消新增");
    },
    // 确认新增按钮
    addRole() {
      if (this.roleList.rname == "" || this.roleList.code == "") {
        this.$message({
          message: "请填写必填项",
          type: "warning",
        });
      } else {
        this.$emit("addRole", this.roleList, false);
        this.roleList = { rname: "", state: 0, code: "", describe: "" };
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.el-input {
  width: 300px;
}
</style>
